<?php include("header.php"); ?>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" enctype="multipart/form-data">
<label for="file">Profile Picture:</label>
<input type="file" name="file" id="file"/><br>
<input type="submit" name="submit" value="Upload!"/>
</form>
<?php
// get file parameters
$filename = $_FILES["file"]["name"];
$error = $_FILES["file"]["error"];
$type = $_FILES["file"]["type"];
$size = $_FILES["file"]["size"];
$loc = $_FILES["file"]["tmp_name"];

// get file extension
$ext = explode(".", $filename);
$ext = $ext[1];

// list allowed extensions
$allowedExt = array("gif","png","jpg","jpeg","png");

// check if extension is allowed
if(in_array($ext, $allowedExt) && $size < 500000) {

	// check file upload errors
	if($error > 0 ) {
		echo "Error: " . $error . "<br>";
	} else {
		// check file already exists
		if(file_exists("upload/" . $filename)) {
			echo $filename . " already exists!";
		}
		// upload the file
		else {
			move_uploaded_file($loc, "upload/" . $filename);
			echo "Stored in: " . "upload/" . $filename;
		}
	}
} else {
	echo "Invalid file type! Must be an image less than 500kB.";
}

?>
<?php include("footer.php"); ?>